package com.zzty.txj1.util;

import com.zzty.txj1.config.WeiChatConfig;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

public class MenuUtil {

    static String appid=WeiChatConfig.appid;
    public static void main(String[] args) throws Exception{
        String url1="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ appid+"&redirect_uri=";           //String url2="&redirect_uri=";
        String url2="&response_type=code&scope=snsapi_base&state=123#wechat_redirect";


        String login=WeiChatConfig.url+"/page/getUserInfo";
        String ranking=WeiChatConfig.url+"/page/ranking";
        String openMap=WeiChatConfig.url+"/page/openMap";
        String luShu=WeiChatConfig.url+"/page/luShu";
        String location=WeiChatConfig.url+"/page/getLocation";
        String record=WeiChatConfig.url+"/page/todayRecord";
        login=URLEncoder.encode(login, "utf-8");

        String bing="http://www.bing.com/";
        //bing=URLEncoder.encode(bing, "utf-8");
        String menu ="{\"button\":[{\"type\":\"view\",\"name\":\"打卡\",\"url\":\""+url1+openMap+url2+"\"} ," +
                "{\"type\":\"view\",\"name\":\"记录\",\"url\":\""+url1+record+url2+"\"}," +
                "{\"type\":\"view\",\"name\":\"排名\",\"url\":\""+url1+ranking+url2+"\"} ]}";

        String access_token=WeiChatUtil.getAccessToken();
        System.out.println("access_token=="+access_token);
        System.out.println("menu "+menu);
        String action = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token;
        try {
            URL url = new URL(action);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();

            http.setRequestMethod("POST");
            http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒
            http.connect();
            OutputStream os= http.getOutputStream();
            os.write(menu.getBytes("UTF-8"));//传入参数
            os.flush();
            os.close();

            InputStream is =http.getInputStream();
            int size =is.available();
            byte[] jsonBytes =new byte[size];
            is.read(jsonBytes);
            String message=new String(jsonBytes,"UTF-8");
            System.out.println("message is "+message);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
